<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\UserProject
 *
 * @property int $id
 * @property int|null $project_id
 * @property int|null $admin_user_id
 * @property int|null $create_time
 * @property int|null $update_time
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereAdminUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereProjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereUpdateTime($value)
 * @mixin \Eloquent
 * @property int|null $num
 * @property-read \App\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserProject whereNum($value)
 */
class UserProject extends Model
{
    protected $table = '5kcrm_admin_user_project';
    public $timestamps = null;
    public function user(){
        return $this->hasOne(User::class,'id','admin_user_id');
    }
    public function current($project_id)
    {
       /* if ($project_id!=21&&$project_id!=26&&$project_id!=24&&$project_id!=27&&$project_id!=25&&$project_id!=16&&$project_id!=23&&$project_id!=29&&$project_id!=32&&$project_id!=20) {//中天联科 艾瑞克斯B组
            $userIds = UserProject::where('project_id', $project_id)->pluck('admin_user_id')->toArray();
            return Customer::fenpei($userIds);
        }*/
        $sort = 'asc';
        $projectUser = UserProject::where('project_id', $project_id)->whereHas('user',function($query){
            $query->where('status',1);
        })->where('num', 0)->first();
        if (!$projectUser) {
            UserProject::where('project_id', $project_id)->update(['num'=>0]);
        }
        $projectUser = UserProject::with('user')->whereHas('user',function($query){
            $query->where('status',1);
        })->where('project_id', $project_id)
            ->orderBy('num', $sort)
            ->orderBy('id', $sort)
            ->first();
        if ($projectUser) {
            $projectUser->num = 1 - $projectUser->num;
            $projectUser->save();
            return $projectUser->admin_user_id;
        }
        return 1;
    }
}
